<template>
  <div class="app-container search-list">
    <table-pagination
        ref="el-search-table"
        :columns="columns"
        :fetch="getData"
        :form-options="formOptions"
        :page-sizes="[20, 50, 100]"
        :adaptive = "fixOption"
        height="300"
        list-field="records"
        method="get"
        page-index-key="page"
        page-size-key="pageSize"
        total-field="totalElements"
        type="remote">
      <template #button>
        <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="setKeywordsDialog = true">新增</el-button>
      </template>
      <el-table-column slot="append" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button plain size="mini" type="primary" @click="handleDetail(scope.row)">编辑</el-button>
          <el-button plain size="mini" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
          <el-button plain size="mini" :type="scope.row.keywordType === '0' ? 'primary' :  'warning'" @click="handleSetDefault(scope.row.id)">{{scope.row.keywordType === '0' ? '取消默认' : '设为默认'}}</el-button>
        </template>
      </el-table-column>
    </table-pagination>
    <el-dialog :visible.sync="setKeywordsDialog" title="关键词" :before-close="cancelBtn" width="500px" class="hotword-dialog">
      <el-form ref="keywordsForm" :model="config" :rules="checkRules">
        <el-form-item label="" prop="keyWords">
          <el-input type="textarea" v-model="config.keyWords" clearable maxlength="150" :rows="4" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="submitLoading" type="primary" @click="saveBtn">保存</el-button>
        <el-button @click="cancelBtn">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TablePagination from '_c/Tables/table-pagination';
import {getHotKeyWordsList, addHotKeyWords, deleteHotKeyWords, updateHotKeyWordsStatus, updateHotKeyWords} from '@/api/site';

export default {
    name: 'search-setting-list',
    components: {TablePagination},
    data () {
        return {
            fixOption: {fixedHeader: true},
            setKeywordsDialog: false,
            submitLoading: false,
            isEdit: false,
            formParams: {},
            pageParams: {
                page: 1,
                pageSize: 20
            },
            config: {
                keyWords: ''
            },
            formOptions: {
                showResetBtn: false,
                inline: false,
                labelWidth: 120,
                submitBtnText: '查询',
                size: 'small',
                resetBtnCallback: () => {
                    this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                },
                forms: [
                ]
            },
            checkRules: {
                'keyWords': [
                    {required: true, message: '请输入关键词', trigger: 'blur'}
                ]
            },
            columns: [
                {label: '关键词', prop: 'keyword'}
            ]
        };
    },
    methods: {
        getData (params) {
            this.formParams.siteId = this.siteId;
            return new Promise((resolve, reject) => {
                getHotKeyWordsList({...this.formParams, ...params}).then((res) => {
                    resolve(res);
                }).catch((error) => {
                    reject(error);
                });
            });
        },
        handleDetail (row) {
            this.setKeywordsDialog = true;
            this.config.keyWords = row.keyword;
            this.config.id = row.id;
            this.isEdit = true;
        },
        handleDel (id) {
            this.$confirm('确认删除关键词吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteHotKeyWords(this.siteId, id).then(res => {
                    this.$message({type: 'success', message: '删除成功!'});
                    this.$refs['el-search-table'].searchHandler(true);
                });
            });
        },
        handleSetDefault (id) {
            updateHotKeyWordsStatus({id, siteId: this.siteId}).then(res => {
                this.$message({type: 'success', message: '设置成功!'});
                this.$refs['el-search-table'].searchHandler(true);
            });
        },
        saveBtn () {
            this.$refs['keywordsForm'].validate(valid => {
                if (valid) {
                    this.submitLoading = true;
                    if (this.isEdit) {
                        updateHotKeyWords({id: this.config.id, keyword: this.config.keyWords, siteId: this.siteId}).then(res => {
                            this.$message({type: 'success', message: '更新成功!'});
                            this.cancelBtn();
                            this.$refs['el-search-table'].searchHandler(true);
                        }).catch(() => {
                            this.submitLoading = false;
                        });
                    } else {
                        addHotKeyWords({siteId: this.siteId, keyword: this.config.keyWords}).then(res => {
                            this.$message({type: 'success', message: '保存成功!'});
                            this.cancelBtn();
                            this.$refs['el-search-table'].searchHandler(true);
                        }).catch(() => {
                            this.submitLoading = false;
                        });
                    }
                }
            });
        },
        cancelBtn () {
            this.config = {keyWords: ''};
            this.submitLoading = false;
            this.isEdit = false;
            this.setKeywordsDialog = false;
            this.$refs['keywordsForm'].resetFields();
        }

    },
    computed: {
        siteId () {
            return this.$store.state.user.siteId;
        }
    }
};
</script>

<style lang="scss">
.hotword-dialog{
  .el-dialog__body{
    height: auto;
    overflow: hidden;
  }
}
</style>
